package wml.clipstore.base;

import java.lang.reflect.Constructor;

import CLIPSJNI.InstanceAddressValue;
import CLIPSJNI.PrimitiveValue;

public class RawEntity {
	private InstanceAddressValue address;

	private RawEntity(InstanceAddressValue address) {
		this.address = address;
	}

	public Object get(String field) throws Exception {
		PrimitiveValue pv = address.directGetSlot("f_"+field);
		Object value = null;
		if(pv instanceof InstanceAddressValue){
			try {
				Constructor<RawEntity> c = RawEntity.class
						.getDeclaredConstructor(InstanceAddressValue.class);
				c.setAccessible(true);
				value = c.newInstance(pv);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			int index = pv.toString().lastIndexOf('@');
			if(index >= 0)
				value = pv.toString().substring(0, index);
		}
		return value;
	}
	
	public String toString(){
		return address.toString();
	}
}
